package com.zyb.system.systemdictionary.controller;

import com.zyb.system.systemdictionary.domain.SystemDictionaryType;
import com.zyb.system.systemdictionary.query.SystemDictionaryTypeQuery;
import com.zyb.system.systemdictionary.service.ISystemDictionaryTypeService;
import com.zyb.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
@Api(value = "数据字典的API",description="数据字典相关的CRUD功能")
public class TypeController {
    @Autowired
    private ISystemDictionaryTypeService systemDictionaryTypeService;
    @GetMapping("/list")//方式二
    @ApiOperation(value = "查询所有字典所有数据")
    public List<SystemDictionaryType> queryAll(){
        return systemDictionaryTypeService.queryAll();
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "查询一条数据")
    public AjaxResult queryOne(@PathVariable Long id){
        try {
            SystemDictionaryType data = systemDictionaryTypeService.queryById(id);
            return AjaxResult.success().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取数据失败");
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "数据字典分页查询")
    public AjaxResult pageList(@RequestBody SystemDictionaryTypeQuery query){
        try {
            List<SystemDictionaryType> page = systemDictionaryTypeService.queryPage(query);
            Long count = systemDictionaryTypeService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取分页数据失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            systemDictionaryTypeService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "新增【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryType systemDictionaryType){
        try {
            if(systemDictionaryType.getId() == null){
                systemDictionaryTypeService.add(systemDictionaryType);
                return AjaxResult.success().setMsg("添加成功");
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
                return AjaxResult.success().setMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("保存数据失败");
    }

    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
}
